(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.finance.modifyController', modifyController);
	modifyController.$inject = [ '$q', '$scope', 'Constants', 'arithService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'piDiscountService', '$filter'];
	function modifyController($q, $scope, Constants, arithService, modalService, gridService, layoutConfig, validatorConfig, authService, piDiscountService, $filter) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piDiscountRequest = {};
		ctrl.amtChangedInv = {};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, rowSelectionChanged);
			ctrl.gridOptions.data = 'vm.piDiscountRequest.discountInvoiceList';
			ctrl.gridOptions.multiSelect = true;
			ctrl.piDiscountRequest = params;
			ctrl.piDiscountRequest.discountInvoiceList = params.discountInvoiceList;
			
			angular.forEach(params.discountInvoiceList, function(data) {
				if(data.communityCodeQ != null && data.communityCode != null){
					data.community = data.communityCodeQ + ":" + data.communityCode;
				}
				//change finance Amt
				if(ctrl.amtChangedInv.hasOwnProperty($filter('encode')(data.invoiceId))){
					data.financeAmt = ctrl.amtChangedInv[$filter('encode')(data.invoiceId)];
				}else{
					data.financeAmt = data.invoiceOutsAmt;
				}
			});
			ctrl.piDiscountRequest.financeBy = params.financeBy;
		};
		


		ctrl.createContinue = function() {
			if (!ctrl.piDiscountRequest.discountInvoiceList || ctrl.piDiscountRequest.discountInvoiceList.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			var params = {
					'selectedRows' : ctrl.piDiscountRequest.discountInvoiceList,
					'amtChangedInv' : ctrl.amtChangedInv,
					'financeBy' : ctrl.piDiscountRequest.financeBy
			};
			if(ctrl.piDiscountRequest.isFutureDate){
				return authService.createContinue(ctrl.piDiscountRequest).then(successful, failed);
			}else{
				return authService.createPreview(ctrl.piDiscountRequest).then(successful, failed);
			}
		};
		
		function failed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function successful(response) {
			if (response.data && response.data.model) {
				var piDiscountRequest = response.data.model;
			}
			var params = {
					'selectedRows' : ctrl.selectedRows,
					'amtChangedInv' : ctrl.amtChangedInv,
					'piDiscountRequest' : piDiscountRequest,
					'financeBy' : ctrl.piDiscountRequest.financeBy
			};
			if(ctrl.piDiscountRequest.isFutureDate){
				$scope.go('apf.finance.create', params);
			}else{
				$scope.go('apf.finance.preview',params);
			}
		}

		ctrl.addInvoice = function(row) {
			return $q(function(resolved, rejected){
				var params = {"multiSelect": "true"};
				params.invoiceIds = [];
				params.dataType = "PI-DISCOUNT";
				angular.forEach(ctrl.piDiscountRequest.discountInvoiceList, function(invoice, index) {
					params.invoiceIds.push($filter('encode')(invoice.invoiceId));
				});
				params.payCcy = ctrl.piDiscountRequest.loanCcy;
				modalService.openCustomDialogs({
					'dialogsModule' : 'apf.invoice',
					'params' : params,
					'btnCallback' : {
						'ok' : function(selectedRows) {
							if (!selectedRows || selectedRows.length == 0) {
								modalService.openErrorDialogs({
									messages : [ 'title.dialogs.info.norecsel' ]
								});
								return false;
							}
							if (!ctrl.piDiscountRequest.discountInvoiceList) {
								ctrl.piDiscountRequest.discountInvoiceList = [];
							}
							angular.forEach(selectedRows, function(selectedObj, index) {
								selectedRows[index].dataStatus = 'C';
								if(selectedObj.communityCode != null && selectedObj.communityCodeQ != null){
									selectedRows[index].community = selectedObj.communityCode + ":" + selectedObj.communityCodeQ;
								}
								selectedRows[index].financeAmt = selectedObj.invoiceOutsAmt;
								ctrl.piDiscountRequest.discountInvoiceList.push(selectedRows[index]);
							});
							
							event.preventDefault();
							event.stopPropagation();
						},
						'close' : function(){
							rejected();
						}
					}
				});
			});
			
		};
		
		ctrl.changeFinanceAmt = function(value, formCtrl, isInitialized){
			if(!isInitialized){
				var invoiceId = $filter('encode')(value.invoiceId);
				var financeAmt = $filter('encode')(value.financeAmt);
				ctrl.amtChangedInv[invoiceId] = financeAmt;
				ctrl.piDiscountRequest.totalFinanceAmt = 0;
				angular.forEach(ctrl.selectedRows, function(value, key) {
					ctrl.piDiscountRequest.totalFinanceAmt = parseFloat(arithService.add(ctrl.piDiscountRequest.totalFinanceAmt, value.financeAmt));
				});
			};
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedInvoices = selectedRows;
		}
		
	}
})(angular);